package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.*;
import com.project.webpage.mapper.ShiftsOntimeDetailMapper;
import com.project.webpage.mapper.ShiftsOntimeMapper;
import com.project.webpage.service.ShiftsOntimeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShiftsOntimeServiceImpl implements ShiftsOntimeService {

    @Resource
    private ShiftsOntimeMapper shiftsOntimeMapper;
    @Resource
    private ShiftsOntimeDetailMapper shiftsOntimeDetailMapper;

    @Override
    @DS("secondary")
    public List<ShiftsOntimeDto> getListForPage(ShiftsOntimeParam param) {
        return shiftsOntimeMapper.selectListByUid(param);
    }

    @Override
    @DS("secondary")
    public List<ShiftsOntimeDto> getDailyListForPage(ShiftsOntimeDailyParam param) {
        return shiftsOntimeMapper.selectDailyListByUid(param);
    }

    @Override
    @DS("secondary")
    public List<ShiftsOntimeDto> getMonthlyListForPage(ShiftsOntimeMonthlyParam param) {
        return shiftsOntimeMapper.selectMonthlyListByUid(param);
    }

    @Override
    @DS("secondary")
    public List<ShiftsOntimeDetailDto> getDetail(SearchByRqParam param) {
        List<ShiftsOntimeDetailDto> list = shiftsOntimeDetailMapper.selectListDetail(param);
        if (list.size() > 0) {
            for (ShiftsOntimeDetailDto dto : list) {
                if ("不准点".equals(dto.getOntime()) && !StringUtils.isEmpty(dto.getSjfcsj())) {
                    String mark = "";
                    long jhfcTime = dto.getJhfcsj_time().getTime();
                    long sjfcTime = dto.getSjfcsj_time().getTime();
                    long diffSeconds = (sjfcTime - jhfcTime) / 1000;
                    if (diffSeconds < 0) {
                        mark = "-";
                        diffSeconds = Math.abs(diffSeconds);
                    }
                    long diffMins = diffSeconds / 60;
                    long reSeconds = diffSeconds % 60;
                    dto.setTimediff(mark + String.valueOf(diffMins) + "分" + String.valueOf(reSeconds) + "秒");
                }
            }
        }
        return list;
    }
}
